<template>
  <Dialog :title="t('doubleChioce.detail.title')" v-model="dialogVisible" width="1400">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item :label="t('systemUser.fields.nickname')" prop="mentorName">
          <el-input
            v-model="queryParams.mentorName"
            :placeholder="t('common.inputTextPlaceholder') + t('systemUser.fields.nickname')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item :label="t('systemUser.fields.jobCode')" prop="mentorJobCode">
          <el-input
            v-model="queryParams.mentorJobCode"
            :placeholder="t('common.inputTextPlaceholder') + t('systemUser.fields.jobCode')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px" />
            {{ t('common.search') }}
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" />
            {{ t('common.reset') }}
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <vxe-table
        id="iDoubleChioce-table"
        v-loading="loading"
        :data="list"
        stripe
        :custom-config="vxeTableConfig"
        :column-config="{ resizable: true }"
        :size="vxeTableSize"
      >
        <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
        <vxe-column
          :title="t('doubleChioce.fields.username')"
          field="username"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorName')"
          field="mentorName"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorJobCode')"
          field="mentorJobCode"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorPhone')"
          field="mentorPhone"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorEmail')"
          field="mentorEmail"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorIdentity')"
          field="mentorIdentity"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorGender')"
          field="mentorGender"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorTitle')"
          field="mentorTitle"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorDept')"
          field="mentorDept"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorWorkStatus')"
          field="mentorWorkStatus"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        >
          <template #default="scope">
            <dict-tag
              v-if="scope.row.mentorWorkStatus"
              :type="DICT_TYPE.WORKING_STATUS"
              :value="scope.row.mentorWorkStatus"
            />
          </template>
        </vxe-column>
        <vxe-column
          :title="t('doubleChioce.fields.useQuota')"
          field="useQuota"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.starter')"
          field="starter"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.studentStatus')"
          field="studentStatus"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('doubleChioce.fields.mentorStatus')"
          field="mentorStatus"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('common.operate')"
          field="operator"
          align="center"
          fixed="right"
          width="160"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleDeleteChoice(scope.row)"
              v-hasPermi="['recruit:double-chioce:update']"
              :disabled="!(scope.row.studentStatus === '是' && scope.row.mentorStatus === '是')"
            >
              {{ t('doubleChioce.index.deleteChoice') }}
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDeleteChoiceInvite(scope.row)"
              v-hasPermi="['recruit:double-chioce:update']"
              :disabled="scope.row.studentStatus === '是' && scope.row.mentorStatus === '是'"
            >
              {{ t('doubleChioce.index.deleteInvite') }}
            </el-button>
          </template>
        </vxe-column>
      </vxe-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>

<script setup lang="ts">
import { vxeDateFormatter } from '@/utils/formatTime'
import { DoubleChioceApi, DoubleChoiceDetailVO } from '@/api/recruit/doublechioce'
import type { VxeTablePropTypes } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { DICT_TYPE } from '@/utils/dict'

/** 双选 列表 */
defineOptions({ name: 'DoubleChoiceDetail' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示

const loading = ref(true) // 列表的加载中
const list = ref<DoubleChoiceDetailVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  recruitStudentId: undefined,
  mentorName: undefined,
  mentorJobCode: undefined
})
const queryFormRef = ref() // 搜索的表单

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  queryParams.recruitStudentId = studentId.value as any
  try {
    const data = await DoubleChioceApi.getDoubleChioceDetailPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const studentId = ref('')
const open = async (id: string) => {
  studentId.value = id
  list.value = []
  total.value = 0
  dialogVisible.value = true
  await nextTick()
  resetQuery()
}

const handleDeleteChoice = async (row: DoubleChoiceDetailVO) => {
  try {
    await message.confirm(t('doubleChioce.detail.confirmTips'))
  } catch {
    return
  }
  try {
    loading.value = true
    await DoubleChioceApi.revokeDoubleChioce(row.id)
  } finally {
    loading.value = false
  }
  message.success(t('doubleChioce.detail.cancelSuccess'))
  emit('revoked')
  await getList()
}

const handleDeleteChoiceInvite = async (row: DoubleChoiceDetailVO) => {
  try {
    await message.confirm(t('doubleChioce.detail.confirmTips2'))
  } catch {
    return
  }
  try {
    loading.value = true
    await DoubleChioceApi.revokeDoubleChioceInvite(row.id)
  } finally {
    loading.value = false
  }
  message.success(t('doubleChioce.detail.cancelSuccess'))
  await getList()
}

const emit = defineEmits(['revoked']) // 定义 success 事件，用于操作成功后的回调

defineExpose({
  open
})
</script>
